package model.quadtree.graph;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

import model.quadtree.Node;
import model.quadtree.Type;


public class Node2Graph extends Node {
	private Node2Graph[]		child;

	public Node2Graph[] getChild() {
		return child;
	}

	public void setChild(Node2Graph[] child) {
		this.child = child;
	}

	public Node2Graph(Type type, Point vert1, Point vert2, Node2Graph parent) {
		super(type, vert1, vert2, parent);
	}
	
	public Node2Graph(Type type, Point vert1, Point vert2) {
		super(type, vert1, vert2);
	}
	
	public Node2Graph(Node node){
		super(node.getType(), node.getVert1(), node.getVert2(), node.getParent());
	}
	
	public void classifyNode2Graph(List<Rectangle> obstacles){
		if(this.type!=Type.GREY){
			int x = vert2.x, y = vert1.y, width = vert1.x-vert2.x, height = vert2.y-vert1.y;
			Rectangle cell = new Rectangle(x, y, width, height);
			for(Rectangle obstacle : obstacles){
				if(obstacle.contains(cell))
					this.type = Type.BLACK;
				//else if(obstacle.intersects(x, y, width, height) || cell.contains(obstacle)){
				else if(obstacle.intersects(x, y, width, height)){
					this.type = Type.GREY;
				}
			}
			done=true;
		}
	}
	
	@Override
	public void makeChildren(){
		child = new Node2Graph[4];
		int x1 = this.vert1.x, y1 = this.vert1.y;
		int x2 = this.vert2.x, y2 = this.vert2.y;
		int xMiddle = (x1+x2)/2, y2Half = (y1+y2)/2;

		Point p01 = new Point(xMiddle, y1);
		Point p02 = new Point(x2, y2Half);
		Point p11 = this.vert1;
		Point p12 = new Point(xMiddle, y2Half);
		Point p21 = p12;
		Point p22 = this.vert2;
		Point p31 = new Point(x1, y2Half);
		Point p32 = new Point(xMiddle, y2);

		child[0] = new Node2Graph(Type.WHITE, p01, p02, this);
		child[1] = new Node2Graph(Type.WHITE, p11, p12, this);
		child[2] = new Node2Graph(Type.WHITE, p21, p22, this);
		child[3] = new Node2Graph(Type.WHITE, p31, p32, this);
		
		this.leaf = false;
		this.type = Type.GREY;
	}
}
